package com.aimosheng.main.activity;

import android.app.Dialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aimosheng.common.CommonAppConfig;
import com.aimosheng.common.activity.AbsActivity;
import com.aimosheng.common.bean.UserBean;
import com.aimosheng.common.glide.ImgLoader;
import com.aimosheng.common.http.HttpCallback;
import com.aimosheng.common.interfaces.CommonCallback;
import com.aimosheng.common.interfaces.ImageResultCallback;
import com.aimosheng.common.upload.FileUploadManager;
import com.aimosheng.common.upload.UploadBean;
import com.aimosheng.common.upload.UploadCallback;
import com.aimosheng.common.upload.UploadStrategy;
import com.aimosheng.common.utils.DialogUitl;
import com.aimosheng.common.utils.ProcessImageUtil;
import com.aimosheng.common.utils.ToastUtil;
import com.aimosheng.common.utils.WordUtil;
import com.aimosheng.main.R;
import com.aimosheng.main.http.MainHttpConsts;
import com.aimosheng.main.http.MainHttpUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cxf on 2018/9/29.
 * 我的 编辑资料
 */

public class EditProfileActivity extends AbsActivity implements View.OnClickListener {

    private ImageView mAvatar;
    private EditText mName;
    private ProcessImageUtil mImageUtil;
    private File mAvatarFile;
    private String mAvatarRemoteFileName;
    private String mNameVal;
    private UploadStrategy mUploadStrategy;
    private Dialog mLoading;
    private UserBean mUserBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_profile;
    }

    @Override
    protected void main() {
        setTitle(WordUtil.getString(R.string.edit_profile));
        mAvatar = findViewById(R.id.avatar);
        mName = findViewById(R.id.name);
        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.btn_avatar).setOnClickListener(this);
        mImageUtil = new ProcessImageUtil(this);
        mImageUtil.setImageResultCallback(new ImageResultCallback() {
            @Override
            public void beforeCamera() {

            }

            @Override
            public void onSuccess(File file) {
                if (file != null) {
                    ImgLoader.display(mContext, file, mAvatar);
                    mAvatarFile = file;
                }
            }

            @Override
            public void onFailure() {
            }
        });
        UserBean u = CommonAppConfig.getInstance().getUserBean();
        if (u != null) {
            showData(u);
        } else {
            MainHttpUtil.getBaseInfo(new CommonCallback<UserBean>() {
                @Override
                public void callback(UserBean u) {
                    showData(u);
                }
            });
        }
    }

    private void showData(UserBean u) {
        mUserBean = u;
        ImgLoader.displayAvatar(mContext, u.getAvatar(), mAvatar);
        String name = u.getUserNiceName();
        if (!TextUtils.isEmpty(name)) {
            if (name.length() > 7) {
                name = name.substring(0, 7);
            }
            mName.setText(name);
            mName.setSelection(name.length());
        }
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_save) {
            save();
        } else if (i == R.id.btn_avatar) {
            editAvatar();
        }
    }

    private void editAvatar() {
        DialogUitl.showStringArrayDialog(mContext, new Integer[]{
                R.string.camera, R.string.alumb}, new DialogUitl.StringArrayDialogCallback() {
            @Override
            public void onItemClick(String text, int tag) {
                if (tag == R.string.camera) {
                    mImageUtil.getImageByCamera();
                } else {
                    mImageUtil.getImageByAlumb();
                }
            }
        });
    }


    /**
     * 保存
     */
    public void save() {
        mNameVal = mName.getText().toString().trim();
        if (TextUtils.isEmpty(mNameVal)) {
            ToastUtil.show(R.string.edit_profile_name_empty);
            return;
        }
        if (mUserBean != null && mNameVal.equals(mUserBean.getUserNiceName()) && mAvatarFile == null) {
            ToastUtil.show(R.string.edit_profile_not_update);
            finish();
            return;
        }
        if (mAvatarFile != null) {
            uploadAvatarImage();
        } else {
            submit();
        }
    }

    /**
     * 上传头像
     */
    private void uploadAvatarImage() {
        mLoading = DialogUitl.loadingDialog(mContext);
        mLoading.show();
        FileUploadManager.getInstance().createUploadImpl(mContext, new CommonCallback<UploadStrategy>() {
            @Override
            public void callback(UploadStrategy strategy) {
                if (strategy == null) {
                    ToastUtil.show(WordUtil.getString(R.string.upload_type_error));
                    return;
                }
                mUploadStrategy = strategy;
                List<UploadBean> list = new ArrayList<>();
                list.add(new UploadBean(mAvatarFile));
                mUploadStrategy.upload(list, true, new UploadCallback() {
                    @Override
                    public void onFinish(List<UploadBean> list, boolean success) {
                        if (success) {
                            if (list != null && list.size() > 0) {
                                mAvatarRemoteFileName = list.get(0).getRemoteFileName();
                            }
                        }
                        submit();
                    }
                });
            }
        });
    }

    private void submit() {
        MainHttpUtil.updateUserInfo(mAvatarRemoteFileName, mNameVal, new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String[] info) {
                if (code == 0 && info.length > 0) {
                    JSONObject obj = JSON.parseObject(info[0]);
                    UserBean u = CommonAppConfig.getInstance().getUserBean();
                    if (u != null) {
                        u.setUserNiceName(obj.getString("user_nickname"));
                        u.setAvatar(obj.getString("avatar"));
                        u.setAvatarThumb(obj.getString("avatar_thumb"));
                    }
                    finish();
                }
                ToastUtil.show(msg);
            }

            @Override
            public void onFinish() {
                if (mLoading != null) {
                    mLoading.dismiss();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        MainHttpUtil.cancel(MainHttpConsts.UPDATE_USER_INFO);
        super.onDestroy();
    }
}
